package com.tedu.food.sys.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.tedu.food.common.vo.JsonResult;
import com.tedu.food.sys.entity.OrderMaster;
import com.tedu.food.sys.service.OrderMasterService;

@RestController
@RequestMapping("/order/")
public class OrderMasterController {

	@Autowired
	private OrderMasterService orderMasterService;

	@PostMapping("doSaveOrderMaster")
	public JsonResult doSaveOrderMaster(OrderMaster entity, Integer[] goodsIds) {
		orderMasterService.insertOrderMaster(entity, goodsIds);
		if (entity.getOrderStatus() == 1)
			return new JsonResult("下单成功");
		return new JsonResult("此订单为未支付订单,会在30分钟后自动取消");
	}

	@PostMapping("doUpdateOrderMasterStatus")
	public JsonResult doUpdateOrderMasterStatus(Integer id) {
		orderMasterService.updateOrderMasterStatus(id);
		return new JsonResult("下单成功");
	}

	@GetMapping("dofindOrderMasterByOrderId")
	public JsonResult dofindOrderMasterByOrderId(Integer id) {
		return new JsonResult(orderMasterService.findOrderMasterByOrderId(id));
	}

	@GetMapping("doFindOrderMastersByStatus")
	public JsonResult doFindOrderMastersByStatus(Integer orderStatus) {
		System.out.println(orderStatus);
		return new JsonResult(orderMasterService.findOrderMastersByStatus(orderStatus));
	}

	@RequestMapping("doFindPageOrders")
	public JsonResult doFindPageOrders(Integer pageCurrent, String orderType) {
		System.out.println("orderStatus=" + orderType + ",pageCurrent=" + pageCurrent);
		return new JsonResult(orderMasterService.findPageOrders(orderType, pageCurrent));
	}

	@RequestMapping("doChangeStatus")
	public JsonResult doChangeStatus(Integer id) {
		orderMasterService.updateOrderStatusById(id);
		return new JsonResult("update ok|修改成功");
	}

	/**
	 * 用户取消待支付订单
	 * @param id  订单id
	 * @return
	 */
	@PostMapping("doCancleOrderMaster")
	public JsonResult doCancleOrderMaster(Integer id){
		orderMasterService.cancleOrderMaster(id);
		return new JsonResult("取消成功");
	}

}